package cn.com.oyat.idea.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import cn.com.oyat.idea.service.IUserService;
import cn.com.oyat.idea.common.AjaxResult;
import cn.com.oyat.idea.common.LayuiPage;
import cn.com.oyat.idea.model.User;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fuce
 * @since 2025-02-20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/idea/user")
public class TUserController {
    private final IUserService iUserService;


    @PostMapping("/login")
    public AjaxResult doLogin(@RequestBody  User user, HttpSession session) {
        User olauser = iUserService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, user.getUsername())
                .eq(User::getPassword, user.getPassword())
        );
        // 这里可以添加登录验证逻辑
        if (olauser != null) {
            // 登录成功，将用户信息存入 session
            session.setAttribute("user", olauser);
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 列表
     *
     * @param vo
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public LayuiPage<User> list(User vo, int page, int limit) {
        Page<User> pager = new Page<>(page, limit);
        IPage<User> iPage = iUserService.queryPageList(pager, vo);
        return new LayuiPage<>(iPage.getTotal(), iPage.getRecords());
    }


    /**********新增方法*************/
    @GetMapping("/add")
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("xxx/add");
        return mv;
    }

    @PostMapping("/add")
    public AjaxResult add(User vo) {
        if (iUserService.save(vo)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**********修改方法*************/
    @GetMapping("/update")
    public ModelAndView updateview(User vo, ModelMap map) {
        User bo = iUserService.getById(vo);
        map.put("bo", bo);

        ModelAndView mv = new ModelAndView("xxx/edit");
        return mv;
    }

    @PutMapping("/update")
    public AjaxResult updateview(User vo) {
        if (iUserService.updateById(vo)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**********删除方法*************/
    @DeleteMapping("/delete")
    public AjaxResult delete(User vo) {
        if (iUserService.removeById(vo)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    /**********下面写其他逻辑方法*************/

}
